package wxapp

import (
	"context"
	"hotgo/internal/dao"
	"hotgo/internal/model/entity"
	"hotgo/internal/service"
	"sync"
)

type sWxapp struct {
	sync.Mutex
}

func init() {
	service.RegisterWxapp(NewWxapp())
}

func NewWxapp() *sWxapp {
	return &sWxapp{sync.Mutex{}}
}

// --  @# logic共用方法- 检查user是否存在
func (s *sWxapp) userIsExist(ctx context.Context, openid string) (*entity.WxUser, bool, error) {
	var theUser entity.WxUser
	theUsers, nums, err := dao.WxUser.Ctx(ctx).Where(dao.WxUser.Columns().Openid, openid).AllAndCount(true)
	if nums > 0 {
		theUser.Openid = theUsers[0]["openid"].String()
		theUser.Phone = theUsers[0]["phone"].String()
	}
	return &theUser, nums > 0, err
}
